<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\AdminUser;
use Omnipay\Omnipay;
use think\Controller;
use think\Db;
use think\Request;

class Recharge extends Base
{


    /**
     * @param Request $request
     * @return \think\response\View
     * 充值首页
     */
    public function index(Request $request)
    {
        $limit      = $request->param('limit') ?: 10;
        $page       = $request->param('page') ?: 1;
        $start_time = $request->param('start_time') ?: '';
        $end_time   = $request->param('end_time') ?: '';
        $keyword    = trim($request->param('keyword')) ?: '';
        $city       = $request->param('city') ?: '';

        $start_recharge = $request->param('start_recharge') ?: '';
        $end_recharge   = $request->param('end_recharge') ?: '';
        $where          = [];
        $whereOr        = [];
        $where_other = [];
        if ($start_time && $end_time) {
            $time1                              = strtotime($start_time);
            $time2                              = strtotime($end_time);
            $where['ad_user_money.create_time'] = ['between', "$time1,$time2"];
            $where_other['create_time'] = ['between', "$time1,$time2"];
        }

        if ($start_recharge && $end_recharge) {
            $where['ad_user_money.money'] = ['between', "$start_recharge,$end_recharge"];
        }

        if ($city) {
            $where['ad_user_info.city'] = $city;
        }

        if ($keyword) {
            $where['ad_user_info.true_name'] = ['like', "%{$keyword}%"];
            $whereOr['ad_user.user_name']    = ['like', "%{$keyword}%"];
        }

        $city_arr = Db::table('ad_user_money')->where(['ad_user_money.status' => 1])
            ->join('ad_user_info', 'ad_user_info.uid = ad_user_money.uid',
                'LEFT')->field(['city'])->distinct('ad_user_info.city')->select();
        foreach ($city_arr as $item => $value) {
            $city_arr[$item]['cityname'] = Db::table('ad_cities')->where(['cityid' => $value['city']])->value('city');
        }
        $recharge_list = Db::table('ad_user_money')->where($where)->whereOr($whereOr)->where(['ad_user_money.status' => 1])
            ->join('ad_user_info', 'ad_user_info.uid = ad_user_money.uid', 'LEFT')
            ->join('ad_user', 'ad_user.id = ad_user_money.uid', 'LEFT')
            ->field([
                'ad_user_money.money',
                'ad_user_money.channel',
                'ad_user_money.id',
                'ad_user_money.create_time',
                'ad_user_info.province',
                'ad_user_info.city',
                'ad_user_info.true_name',
                'ad_user.user_name as phone',
                'ad_user_info.job_one',
                'ad_user_info.job_two',
                'ad_user_info.job_three',
            ])
            ->paginate($limit, false,
                [
                    'page' => $page,
                    'query' => [
                        'keyword' => $keyword,
                        'start_time' => $start_time,
                        'end_time' => $end_time,
                        'start_recharge' => $start_recharge,
                        'end_recharge' => $end_recharge,
                        'city_arr' => $city_arr,
                        'city' => $city,
                    ]
                ])->each(function ($item, $key) {
                $item['province_name']  = Db::table('ad_provinces')->where(['provinceid' => $item['province']])->value('province');
                $item['city_name']
                                        = Db::table('ad_cities')->where(['cityid' => $item['city']])->value('city');
                $item['create_time']    = date('Y-m-d H:i:s', $item['create_time']);
                $item['job_three_name'] = Db::table('ad_industry')->where(['id' => $item['job_two']])->value('name') . Db::table('ad_industry')->where(['id' => $item['job_three']])->value('name');
                return $item;
            });
        $recharge = Db::table('ad_user_money')->where($where_other)->where(['status' => 1])->sum('money');
        return view('list',
            compact('recharge_list', 'keyword', 'start_time', 'end_time', 'start_recharge', 'end_recharge', 'city_arr','recharge',
                'city'));
    }


    /**
     * @param Request $request
     * 删除充值记录
     */
    public function delete(Request $request)
    {
        $id = $request->param('id');
        //删除多条
        if (empty($id)) {
            $ids = $_POST['ids'];
            foreach ($ids as $k => $v) {
                Db::table('ad_user_money')->where(['id' => $v])->update(['deleted_at' => 1]);
                $action = "财务提现ID" . $v;
                $this->getLog("2", $action);
            }
            $this->success('删除成功');
        } else {//单条
            if (Db::table('ad_user_money')->where(['id' => $id])->update(['deleted_at' => 1])) {
                $action = "财务提现ID" . $id;
                $this->getLog("2", $action);
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        }
    }

}
